ucode: allow calling udebug.init() multiple times
authorFelix Fietkau <[email protected]>
Tue, 22 Jul 2025 16:06:52 +0000 (18:06 +0200)
committerFelix Fietkau <[email protected]>
Tue, 22 Jul 2025 16:06:56 +0000 (18:06 +0200)
Simplify using it from multiple places within an application

Signed-off-by: Felix Fietkau <[email protected]>
lib-ucode.c

index 2e63c0bc3558d259fae9c08f822bb4d1bda7d4f7..ec8c68a4668e0f367aeada071678c5a66507e999 100644 (file)
@@ -46,12 +46,20 @@ uc_udebug_init(uc_vm_t *vm, size_t nargs)
        uc_value_t *arg = uc_fn_arg(0);
        uc_value_t *flag_auto = uc_fn_arg(1);
        const char *path = NULL;
+       static bool init_done;
 
        if (ucv_type(arg) == UC_STRING)
                path = ucv_string_get(arg);
 
-       udebug_init(&u);
-       u.notify_cb = uc_udebug_notify_cb;
+       if (!init_done) {
+               udebug_init(&u);
+               u.notify_cb = uc_udebug_notify_cb;
+       }
+
+       if (init_done && !path && udebug_is_connected(&u))
+               goto out;
+
+       init_done = true;
        if (flag_auto && !ucv_is_truish(flag_auto)) {
                if (udebug_connect(&u, path))
                        return NULL;
@@ -59,6 +67,7 @@ uc_udebug_init(uc_vm_t *vm, size_t nargs)
                udebug_auto_connect(&u, path);
        }
 
+out:
        return ucv_boolean_new(true);
 }